﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using DocumentBuilder.Controls;
using DocumentBuilder.Controls.Utils;

namespace DocumentBuilder.Components
{
    public class View : IJsonConvertible
    {
        public List<String> Css { get; set; }
        public List<String> Scripts { get; set; }

        public List<IControl> Children { get; private set; }

        public String Html
        {
            get
            {
                var canvas = new StringBuilder();
                foreach(IControl control in Children)
                    control.PaintHtmlString(canvas);

                return canvas.ToString();
            }
        }

        private View()
        {
            Css = new List<string>();
            Scripts = new List<string>();
            Children = new List<IControl>();
        }

        public static View Create(Document context, XmlNode viewNode)
        {
            var view = new View();

            var cssPathsNode = viewNode.SelectSingleNode("./csspaths");
            if (cssPathsNode != null)
            {
                foreach (XmlNode cssNode in cssPathsNode.ChildNodes)
                {
                    if (cssNode.Attributes != null) view.Css.Add(cssNode.Attributes["href"].Value);
                }
            }

            var scriptsNode = viewNode.SelectSingleNode("./scripts");
            if (scriptsNode != null)
            {
                foreach (XmlNode scriptNode in scriptsNode.ChildNodes)
                {
                    if (scriptNode.Attributes != null) view.Scripts.Add( "http://localhost:52646/DocumentBasedSite/" + scriptNode.Attributes["src"].Value);
                }
            }

            var layoutNode = viewNode.SelectSingleNode("./layout");

            /* Parse and add child controls in the collection
             */
            ControlBuilder.ParseChildControls(context, layoutNode, view.Children);

            return view;
        }

        #region IJsonConvertible Members

        public void PaintJsonString(StringBuilder jsonCanvas)
        {
            jsonCanvas.Append("{");

            int cnt = 0;
            foreach (var script in Scripts)
            {
                if (cnt == 0)
                    jsonCanvas.Append("scripts: [");

                jsonCanvas.Append("\"" + script + "\"");

                if (cnt == Scripts.Count - 1)
                    jsonCanvas.Append("],");
                else
                    jsonCanvas.Append(",");

                cnt++;
            }

            cnt = 0;
            foreach (var css in Css)
            {
                if (cnt == 0)
                    jsonCanvas.Append("css: [");

                jsonCanvas.Append("\"" + css + "\"");

                if (cnt == Css.Count - 1)
                    jsonCanvas.Append("], ");
                else
                    jsonCanvas.Append(",");

                cnt++;
            }

            jsonCanvas.Append(string.Format("\"{0}\": \"{1}\"", "html", Html.Replace("\"", "\\\"").Replace(Environment.NewLine, "")));

            jsonCanvas.Append("}");
        }

        #endregion
    }
}